#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author:taotieren


"""
    rpi.gpio 需要 root 权限，板上运行需要使用
    sudo python charlieplexing.py
    6 个 LED 使用 charlieplexing 连接
    ![charlieplexing.png](charlieplexing.png)
"""

import RPi.GPIO as GPIO

""" 非树莓派环境下可以用 rpi.gpio_def 代替 rpi.gpio 来进行程序调试和验证 """
pins = [18, 23, 24]

""" -1: 输入
    0：低电平
    1：高电平
    LEDs = n*n -n
"""
pin_led_states = [
    [1, 0, -1],  # A
    [0, 1, -1],  # B
    [-1, 1, 0],  # C
    [-1, 0, 1],  # D
    [1, -1, 0],  # E
    [0, -1, 1]   # F
]

GPIO.setmode(GPIO.BCM)


def set_pin(pin_index, pin_state):
    if pin_state == -1:
        GPIO.setup(pins[pin_index], GPIO.IN)
    else:
        GPIO.setup(pins[pin_index], GPIO.OUT)
        GPIO.output(pins[pin_index], pin_state)


def light_led(led_number):
    for pin_index, pin_state in enumerate(pin_led_states[led_number]):
        set_pin(pin_index, pin_state)


set_pin(0, -1)
set_pin(1, -1)
set_pin(2, -1)

while True:
    x = int(input("Pin (0 ~ 5):"))
    light_led(x)
